package davion.study.summercloud.controller;

import davion.study.summercloud.model.FileStoreStatistics;
import davion.study.summercloud.model.User;
import davion.study.summercloud.model.UserDTO;
import davion.study.summercloud.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author: Davion
 * @date: 2020/6/19
 * @description:
 */
@Controller
public class AdminController {

    @Autowired
    UserService userService;

    @RequestMapping("/admin")
    public String admin(HttpServletRequest request, Model model){
        User user = (User) request.getSession().getAttribute("user");
        if (user.getAdmin() == 0) {
            return "redirect:/files";
        }
        FileStoreStatistics statistics = (FileStoreStatistics) request.getSession().getAttribute("statistics");
        List<UserDTO> userDTOS = userService.getAllUserInfoExceptSelf(user.getId());
        model.addAttribute("user", user);
        model.addAttribute("statistics", statistics);
        model.addAttribute("userDTOS", userDTOS);
        return "u-admin/admin";
    }

    @RequestMapping("/updateUserLevel")
    public String updateUserLevel(Integer uId, Integer level, Integer size){
        userService.updateUserLevel(uId, level, size);
        return "redirect:/admin";
    }
}
